#include"../logs/viclog.h"
#include<unistd.h>
enum class TimeGap
{
    GAP_SECOND,
    GAP_MINUTE,
    GAP_HOUR,
    GAP_DAY
};
class RollByTimeSink:public viclog::LogSink
{
    public:
        //构造时传入文件名并打开文件，将操作句柄管理起来
        RollByTimeSink(const std::string& basename,TimeGap gap_type)
        :_basename(basename)
        {
            switch (gap_type)
            {
            case TimeGap::GAP_SECOND: _gap_size=1;break;
            case TimeGap::GAP_MINUTE: _gap_size=60;break;
            case TimeGap::GAP_HOUR: _gap_size=3600;break;
            case TimeGap::GAP_DAY: _gap_size=3600*24;break;

            }
            _cur_gap=_gap_size==1?viclog::util::Date::now():viclog::util::Date::now()%_gap_size;
            std::string pathname=createNewFile();
            //创建日志文件所在目录
            viclog::util::File::createDirectory(viclog::util::File::path(pathname));
            //打开文件
            _ofs.open(pathname,std::ios::binary|std::ios::app);
            assert(_ofs.is_open());

        }
        void log(const char* data,size_t len)
        {
            time_t cur=viclog::util::Date::now();
            if((cur%_gap_size)!=_cur_gap)
            {
                _ofs.close();
                std::string filename=createNewFile();
                _ofs.open(filename,std::ios::binary|std::ios::app);
                assert(_ofs.is_open());
            }
            _ofs.write(data,len);
            assert(_ofs.good());

        }
        private:
        //超过指定大小时创建新文件
         std::string createNewFile()
         {
            //获取系统时间，以系统时间创建文件扩展名
            time_t t=viclog::util::Date::now();
            struct tm lt;
            localtime_r(&t,&lt);
            std::stringstream filename;
            filename<<_basename;
            filename<<lt.tm_year+1900;
            filename<<lt.tm_mon+1;
            filename<<lt.tm_mday;
            filename<<lt.tm_hour;
            filename<<lt.tm_min;
            filename<<lt.tm_sec;
            filename<<"-";
            // filename<<_name_count++;
            filename<<".log";
            return filename.str();


         }
        private:
        std::string _basename;//基础文件名（通过基础文件名+扩展文件名（按时间生产）组成当前实际输出文件名）
        //./log/base--->./log/base-20200922089.log
        
        std::ofstream _ofs;//输出文件的操作句柄
        size_t _cur_gap;//超过max_fsize则切换文件
        size_t _gap_size;//记录当前文件大小
};
int main()
{
    std::unique_ptr<viclog::LoggerBuilder> builder(new viclog::GlobalLoggerBuilder());
    builder->buildLoggerName("async_logger");
    builder->buildLoggerLevel(viclog::LogLevel::value::DEBUG);
    builder->buildFormatter("[%c][%f:%l]%m%n");
    builder->buildLoggerType(viclog::LoggerType::LOGGER_SYNC);
    //builder->buildEnableUnSafeAsync();
    builder->buildSink<viclog::FileSink>("./logfile/async.log");
    builder->buildSink<viclog::StdoutSink>();
    builder->buildSink<RollByTimeSink>("./logfile/roll-async-by-time.log",TimeGap::GAP_SECOND);

    viclog::Logger::ptr logger=builder->build();
    size_t cur=viclog::util::Date::now();
    while(viclog::util::Date::now()<(cur+5))
    {
        logger->fatal("一条测试");
        usleep(1000);
    }
   
}